package cn.liyicun.test.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

@Service
public class UserFileService {

    @Autowired
    JdbcTemplate jdbc;



    public long addFile(Long uid,String thumb ,String link,int catid,int level,int viewCount,int likeCount, String extName){
        PreparedStatementCreator psc=con -> {
            String sql="INSERT INTO `user_file_info` (`uid`, `thumb`, `link`, `level`, `catid`, `create_time`, `view_count`, `like_count`, `extName`,status) VALUES (?,?,?,?,?,now(),?,?,?,1)";
            PreparedStatement ps = con.prepareStatement(sql, new String[] { "id" });
            int i=1;
            ps.setLong(i++,uid);
            ps.setString(i++,thumb);
            ps.setString(i++,link);
            ps.setInt(i++,level);
            ps.setInt(i++,catid);
            ps.setInt(i++,viewCount);
            ps.setInt(i++,likeCount);
            ps.setString(i++,extName);
            return ps;};
        KeyHolder holder=new GeneratedKeyHolder();
        jdbc.update(psc,holder);
        //return jdbc.update(sql,user,passwd,new Date(),0,new Date(),new Date(),fullName);
        return holder.getKey().longValue();
    }
    public Long countFile(){
        String sql="select count(*) from user_file_info";
        return jdbc.queryForObject(sql,Long.class);
    }



    public List<Map<String, Object>> myFileList(Long uid){
        String sql="SELECT f.*,i.full_name FROM `user_file_info` f LEFT JOIN user_info i on f.uid=i.id where f.uid=? and f.status=1 order by f.id desc LIMIT 0, 1000";
        return jdbc.queryForList(sql,uid);
    }


    public Map<String, Object> getUserFile(Long id){
        String sql="SELECT f.* FROM `user_file_info` f  where f.id=? ";
        return jdbc.queryForMap(sql,new Long[]{id});
    }


    public void lookLog(Long uid, Long id, String ip){
        String sql="INSERT INTO `user_buy_log` (`uid`, `create_time`, `file_id`, `price`,ip) VALUES (?,now(),?, '0',?)";
        jdbc.update(sql,uid,id,ip);

    }


    public List<Map<String, Object>> squareFileList(String where,String orderby, int limit, int count){
        String sql="SELECT f.*,i.full_name FROM `user_file_info` f LEFT JOIN user_info i on f.uid=i.id where f.status=1 and f.level=0 " ;
        sql+=where;
        sql+=" order by "+orderby;
                sql+=" LIMIT ?, ?";
        return jdbc.queryForList(sql, limit,count);
    }


    public int updateLook(Long id){
        String sql="UPDATE `user_file_info` SET view_count=view_count+1 WHERE (`id`=?)";

        return jdbc.update(sql,id);
    }

    public int updateLike(Long id){
        String sql="UPDATE `user_file_info` SET like_count=like_count+1 WHERE (`id`=?)";
        return jdbc.update(sql,id);
    }

    public List<Map<String, Object>> myFriendList(Long id) {

        String sql="SELECT f.*,i.full_name FROM `user_file_info` f LEFT JOIN user_info i on f.uid=i.id where f.uid=? and f.status=1 and f.level=0 order by f.id desc LIMIT 0, 1000";
        return jdbc.queryForList(sql,id);

    }
}
